//
//  ViewController.m
//  02 英文按首字母排序
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "NH_DataModel.h"

#import "NetWorkingTool.h"
#import "NHOrderByFirstLetter.h"

#define Width self.view.frame.size.width
#define Height self.view.frame.size.height

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
// 保存原始数据
@property(nonatomic, retain)NSMutableArray *dataArr;
// 排序后的数据
@property(nonatomic, retain)NSMutableArray *orderArr;
// 显示数据
@property(nonatomic, retain)UITableView *tableView;
// 判断是有序的还是无序的
@property(nonatomic, assign)BOOL isOrder;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 初始化判断变量
    self.isOrder = NO;
    
    // 设置navigation
    [self setNavigation];
    // 读取数据
    [self createData];
    // 创建视图
    [self createView];
}
#pragma mark 设置Navigation
- (void)setNavigation{
    self.title = @"英文按首字母排序";
    // 创建排序按钮
    UIButton *orderButton = [UIButton buttonWithType:UIButtonTypeSystem];
    orderButton.frame = CGRectMake(0, 0, 40, 40);
    [orderButton setTitle:@"排序" forState:UIControlStateNormal];
    [orderButton addTarget:self action:@selector(orderAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:orderButton];
}

#pragma mark 读取数据
- (void)createData{
    NSString *interface = @"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=199&lat=52.370216&lon=4.895168&oauth_token=64c846531f44c9e5875e8344e3760470&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=B038EC96-77B5-4879-93CD-BD5BEE67AE9A&track_os=ios%25209.2&track_user_id=7325921&v=1";
    self.dataArr = [NSMutableArray array];
    self.orderArr = [NSMutableArray array];
    [NetWorkingTool netWorkingWithStrURL:interface block:^(id result) {
        
        // 原始数据
        self.dataArr = [NH_DataModel baseModelByArr:result[@"data"]];
        
        // 排序
        self.orderArr =  [NHOrderByFirstLetter baseOrderPinYinOnArr:self.dataArr ByKey:@"enname"];
        [self.tableView reloadData];
        
        // 将排序后的内容输出
        //        NSInteger index = 0;
        //        for (NSDictionary *dic in self.orderArr) {
        //            NSLog(@"%@",dic[@"title"]);
        //            for (NH_DataModel *model in dic[@"datas"]) {
        //                NSLog(@"%@,%ld",model.cnname,index);
        //                index ++;
        //            }
        //            NSLog(@"\n\n\n");
        //        }
    }];
}

#pragma mark 创建视图
- (void)createView{
    
    // 创建TableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
}
#pragma mark 实现排序的点击方法
- (void)orderAction:(UIButton *)orderButton{
    // 无序
    if(self.isOrder == NO){
        [orderButton setTitle:@"无序" forState:UIControlStateNormal];
    }
    // 有序
    else{
        [orderButton setTitle:@"排序" forState:UIControlStateNormal];
    }
    self.isOrder = !self.isOrder;
    [self.tableView reloadData];
}

#pragma mark TableView的协议方法
// 个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 无序
    if(self.isOrder == NO){
        return self.dataArr.count;
    }
    // 有序
    else{
        return [self.orderArr[section][@"datas"] count];
    }
}
// 设置Section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    // 无序
    if(self.isOrder == NO){
        return 1;
    }
    // 有序
    else{
        return self.orderArr.count;
    }
}

// 创建Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    // 无序
    if(self.isOrder == NO){
        NH_DataModel *model = self.dataArr[indexPath.row];
        cell.textLabel.text = model.enname;
    }
    // 有序
    else{
        NSArray *datasArr = self.orderArr[indexPath.section][@"datas"];
        NH_DataModel *model =  datasArr[indexPath.row];
        cell.textLabel.text = model.enname;
    }
    return cell;
}
// 设置分区悬浮标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    // 无序
    if(self.isOrder == NO){
        return nil;
    }
    // 有序
    else{
        return self.orderArr[section][@"title"];
    }
}
// 设置分区快捷条
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    // 无序
    if(self.isOrder == NO){
        return nil;
    }
    // 有序
    else{
        NSMutableArray *titleArr = [NSMutableArray array];
        for (NSDictionary *dic in self.orderArr) {
            [titleArr addObject:dic[@"title"]];
        }
        return titleArr;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
